package com.borko.healthcare.tapestry.pages;

import org.apache.tapestry5.ValidationException;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SetupRender;
import org.apache.tapestry5.beaneditor.Validate;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.springframework.context.ApplicationContext;

import com.borko.healthcare.entities.beans.PharmacistBean;
import com.borko.healthcare.entities.beans.SupplierBean;
import com.borko.healthcare.entities.beans.UserDetailsBean;
import com.borko.healthcare.entities.beans.enums.GenderType;
import com.borko.healthcare.entities.interfaces.Pharmacist;
import com.borko.healthcare.entities.interfaces.Supplier;
import com.borko.healthcare.entities.interfaces.UserDetails;
import com.borko.healthcare.persistence.hibernate.services.PharmacistManager;
import com.borko.healthcare.persistence.hibernate.services.SupplierManager;
import com.borko.healthcare.persistence.hibernate.services.UserDetailsManager;


public class Registration {
	

	@SuppressWarnings("unused")
	@Property
    @Persist
    @Validate("required")
    private GenderType genderType;
	
	@Property
	@Persist
	private UserDetails userDetails;
	
	@Inject
	private ApplicationContext applicationContext;
	
	private SupplierManager supplierManager;
	private PharmacistManager pharmacistManager;
	private UserDetailsManager userDetailsManager;
	
	@SetupRender
	public void initialize() {
		if (userDetails == null) {
			userDetails = new UserDetailsBean();
		}
	}
	
	public void onValidateFromRegistrationForm() throws ValidationException {
		
	}
	
	public Object onSuccessFromRegistrationForm() {
		switch (userDetails.getUserType()) {
		case SUPPILER:
			Supplier supplier = new SupplierBean();
			supplier.getUserDetails().setBirthDate(userDetails.getBirthDate());
			supplier.getUserDetails().setEmail(userDetails.getEmail());
			supplier.getUserDetails().setFullName(userDetails.getFullName());
			supplier.getUserDetails().setGender(userDetails.getGender());
			supplier.getUserDetails().setMobilePhone(userDetails.getMobilePhone());
			supplier.getUserDetails().setPassword(userDetails.getPassword());
			supplier.getUserDetails().setUserName(userDetails.getUserName());
			supplier.getUserDetails().setUserType(userDetails.getUserType());
			
			userDetailsManager = (UserDetailsManager) applicationContext.getBean("userDetailsManager");
			userDetailsManager.add(supplier.getUserDetails());
			
			supplierManager = (SupplierManager) applicationContext.getBean("supplierManager");
			supplierManager.add(supplier);
			
			break;

		case PHARMACIST:
			Pharmacist pharmacist = new PharmacistBean();
			pharmacist.getUserDetails().setBirthDate(userDetails.getBirthDate());
			pharmacist.getUserDetails().setEmail(userDetails.getEmail());
			pharmacist.getUserDetails().setFullName(userDetails.getFullName());
			pharmacist.getUserDetails().setGender(userDetails.getGender());
			pharmacist.getUserDetails().setMobilePhone(userDetails.getMobilePhone());
			pharmacist.getUserDetails().setPassword(userDetails.getPassword());
			pharmacist.getUserDetails().setUserName(userDetails.getUserName());
			pharmacist.getUserDetails().setUserType(userDetails.getUserType());
			
			userDetailsManager = (UserDetailsManager) applicationContext.getBean("userDetailsManager");
			userDetailsManager.add(pharmacist.getUserDetails());
			
			pharmacistManager = (PharmacistManager) applicationContext.getBean("pharmacistManager");
			pharmacistManager.add(pharmacist);
			
			break;
			
		default:
			break;
		}
		userDetails = null;
		
		return Login.class;
	}

}
